#include <chipKITEthernet.h>
#include <SPI.h>

/*
  This sketch is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This sketch is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this sketch; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
/************************************************************************/
/*  Revision History:							*/
/* 									*/
/* 8/08/2011 Digilent Inc. (KeithV)					*/
/* Updated for use with the chipKIT Max32 and chipKIT Network Shield	*/
/*									*/
/************************************************************************/

/*
  Web  Server
 
 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)
 
 created 18 Dec 2009
 by David A. Mellis
 modified 4 Sep 2010
 by Tom Igoe
 
 */

// Enter a MAC address and IP address for your controller below. 
// A zero MAC address means that the chipKIT MAC is to be used.
byte mac[] = { 0x00, 0x04, 0xa3, 0x52, 0x90, 0x5b };
byte ip[] = { 192,168,1,60 };

//KTH IP
//byte ip[] = { 130, 237, 20, 125 };

//Array used in function PareReceivedRequest()
char cmd[15];    // Nothing magical about 15, increase these if you need longer commands/parameters
char param1[15];
char param2[15];

//Variables to the Led status on arduino and iar
String AR_L1_ON;
String AR_L1_OFF;
String AR_L2_ON;
String AR_L2_OFF;
String AR_L3_ON;
String AR_L3_OFF;
String AR_L4_ON;
String AR_L4_OFF;
String AR_ERROR = "";
String IAR_L1_ON = "";
String IAR_L1_OFF = "";
String IAR_L2_ON = "";
String IAR_L2_OFF = "";
String IAR_L3_ON = "";
String IAR_L3_OFF = "";
String IAR_L4_ON = "";
String IAR_L4_OFF = "";
char IAR_L1_STATUS = 0;
char IAR_L2_STATUS = 0;
char IAR_L3_STATUS = 0;
char IAR_L4_STATUS = 0;
String IAR_ERROR = "";
char IAR_TEMP[4];
int i = 0;

//Buffer for http-request
#define bufferMax 128
int bufferSize;
char buffer[bufferMax];


// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
Server server(80);

void setup()
{
  //Serial.println("In setup()...");
  //Set the pinmodes on the arduino and their start value
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(78,OUTPUT);
  pinMode(79,OUTPUT);
  pinMode(80,OUTPUT);
  pinMode(81,OUTPUT);
  digitalWrite(8, LOW); 
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
  digitalWrite(78, LOW);
  digitalWrite(79, LOW);
  digitalWrite(80, LOW);
  digitalWrite(81, LOW);
  
  //Start serial and ethernet communication communication
  Serial.begin(9600);
  Serial1.begin(115200);
  Ethernet.begin(mac, ip);
  //Ethernet.begin();  // this will use DHCP
  Serial.println("Starting server...");
  server.begin();
  //Calling function CheckLedStatus()
  Serial.println("Calling CheckLedStatus()...");
  CheckLedStatus();
  Serial.println("Calling CheckTemp()...");
  CheckTemp();
  Serial.println("Calling CheckIarLed()...");
  CheckIarLed();
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    bufferSize = 0;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply

        if (c == '\n' && currentLineIsBlank) {
          
          //calling function ParseReceivedRequest()
          Serial.println("");
          Serial.println("Calling ParseReceivedRequest()...");
          ParseReceivedRequest();
          
          
          
/***************************************************************************************
                         Analog input:
****************************************************************************************/
          if ( strcmp(cmd,"?Temp_Refresh=Refresh") == 0 ) 
          {
            CheckTemp();
          }
/***************************************************************************************
                         Arduino LED1:
****************************************************************************************/
          //LED1 on
          else if ( strcmp(cmd,"?L1=ON") == 0 )
          {
           digitalWrite(8,HIGH);
           if (digitalRead(8) == HIGH) {
             AR_L1_ON = "style='background-color:green;'";
             AR_L1_OFF = "";
           }
             else (AR_ERROR = "LED1 error");
          }
         //LED1 off 
         else if ( strcmp(cmd,"?L1=OFF") == 0 )
         {
           digitalWrite(8,LOW);
           if (digitalRead(8) == LOW) {
            AR_L1_ON = "";
            AR_L1_OFF = "style='background-color:red;'"; 
           }
             else (AR_ERROR = "LED1 error");  
         }
/***************************************************************************************
                         Arduino LED2:
****************************************************************************************/
         //LED2 on
         else if ( strcmp(cmd,"?L2=ON") == 0 )
         {
           digitalWrite(9,HIGH);
           if (digitalRead(9) == HIGH) {
             AR_L2_ON = "style='background-color:green;'";
             AR_L2_OFF = "";
           }
             else (AR_ERROR = "LED2 error");    
         }
         //LED2 off
         else if ( strcmp(cmd,"?L2=OFF") == 0 )
         {
           digitalWrite(9,LOW); 
           if (digitalRead(9) == LOW) {
             AR_L2_ON = "";
             AR_L2_OFF = "style='background-color:red;'"; 
           }
             else (AR_ERROR = "LED2 error");   
         }
/***************************************************************************************
                         Arduino LED3:
****************************************************************************************/
         //LED3 on
         else if ( strcmp(cmd,"?L3=ON") == 0 )
         {
           digitalWrite(10,HIGH);       
           if (digitalRead(10) == HIGH) {
             AR_L3_ON = "style='background-color:green;'";
             AR_L3_OFF = ""; 
           }
             else (AR_ERROR = "LED3 error");    
         }
         //LED3 off
         else if ( strcmp(cmd,"?L3=OFF") == 0 )
         {
           digitalWrite(10,LOW);        
           if (digitalRead(10) == LOW) {
             AR_L3_ON = "";
             AR_L3_OFF = "style='background-color:red;'";  
           }
             else (AR_ERROR = "LED3 error"); 
         }
/***************************************************************************************
                         Arduino LED4:
****************************************************************************************/
         //LED4 on
         else if ( strcmp(cmd,"?L4=ON") == 0 )
         {
           digitalWrite(11,HIGH);   
           if (digitalRead(11) == HIGH) {
             AR_L4_ON = "style='background-color:green;'";
             AR_L4_OFF = "";   
           }
             else (AR_ERROR = "LED4 error");   
         }
         //LED4 off
         else if ( strcmp(cmd,"?L4=OFF") == 0 )
         {
           digitalWrite(11,LOW);        
           if (digitalRead(11) == LOW) {
             AR_L4_ON = "";
             AR_L4_OFF = "style='background-color:red;'";    
           }
             else (AR_ERROR = "LED4 error");  
         }
/***************************************************************************************
                         IAR LED1:
****************************************************************************************/         
         //LED1 on
         else if ( strcmp(cmd,"?IAR_L1=ON") == 0 )
         {
           IAR_L1_STATUS = 0;
           Serial1.write('B');
           Serial.println("'B' sent...");
           Serial1.write('A');
           Serial.println("'A' sent...");
           while (Serial1.available()) {
             IAR_L1_STATUS = Serial1.read();
             Serial.print("IAR_L1_STATUS = ");
             Serial.println(IAR_L1_STATUS);
           }
           if(IAR_L1_STATUS == 'A') {
             IAR_L1_ON = "style='background-color:green;'";
             IAR_L1_OFF = ""; 
             IAR_ERROR = "";
             }
             else (IAR_ERROR = "LED1 error");      
         }
         //LED1 off
         else if ( strcmp(cmd,"?IAR_L1=OFF") == 0 )
         {
           Serial1.write('C');
           Serial.println("'C' sent...");
           Serial1.write('A');
           Serial.println("'A' sent...");
           while (Serial1.available()) {
             IAR_L1_STATUS = Serial1.read();
             Serial.print("IAR_L1_STATUS = ");
             Serial.println(IAR_L1_STATUS);
             }
           if (IAR_L1_STATUS == 'a') {
               IAR_L1_ON = "";
               IAR_L1_OFF = "style='background-color:red;'";
               IAR_ERROR = "";
             }
           else (IAR_ERROR = "LED1 error");   
         }
/***************************************************************************************
                         IAR LED2:
****************************************************************************************/
         else if ( strcmp(cmd,"?IAR_L2=ON") == 0 )
         {
           Serial1.write('E');
           Serial.println("'E' sent...");
           Serial1.write('D');
           Serial.println("'D' sent...");
           while (Serial1.available()) {
             IAR_L2_STATUS = Serial1.read();
             Serial.print("IAR_L2_STATUS = ");
             Serial.println(IAR_L2_STATUS);
             }
           if (IAR_L2_STATUS == 'D') {
               IAR_L2_ON = "style='background-color:green;'";
               IAR_L2_OFF = "";
               IAR_ERROR = "";
             }
           else (IAR_ERROR = "LED2 error");
         }
         else if ( strcmp(cmd,"?IAR_L2=OFF") == 0 )
         {
           IAR_L2_STATUS = 0;
           Serial1.write('F');
           Serial.println("'F' sent...");
           Serial1.write('D');
           Serial.println("'D' sent...");
           while (Serial1.available()) {
             IAR_L2_STATUS = Serial1.read();
             Serial.print("IAR_L2_STATUS = ");
             Serial.println(IAR_L2_STATUS);
             }
           if (IAR_L2_STATUS == 'd') {
               IAR_L2_ON = "";
               IAR_L2_OFF = "style='background-color:red;'";
               IAR_ERROR = "";
             }
           else (IAR_ERROR = "LED2 error");
         }
/***************************************************************************************
                         IAR LED3:
****************************************************************************************/
         else if ( strcmp(cmd,"?IAR_L3=ON") == 0 )
         {
           Serial1.write('H');
           Serial.println("'H' sent...");
           Serial1.write('G');
           Serial.println("'G' sent...");
           while (Serial1.available()) {
             IAR_L3_STATUS = Serial1.read();
             Serial.print("IAR_L3_STATUS = ");
             Serial.println(IAR_L3_STATUS);
             }
           if (IAR_L3_STATUS == 'G') {
               IAR_L3_ON = "style='background-color:green;'";
               IAR_L3_OFF = "";
               IAR_ERROR = "";
             }
           else (IAR_ERROR = "LED3 error");
         }
         else if ( strcmp(cmd,"?IAR_L3=OFF") == 0 )
         {
           Serial1.write('I');
           Serial.println("'I' sent...");
           Serial1.write('G');
           Serial.println("'G' sent...");
           while (Serial1.available()) {
             IAR_L3_STATUS = Serial1.read();
             Serial.print("IAR_L3_STATUS = ");
             Serial.println(IAR_L3_STATUS);
             }
           if (IAR_L3_STATUS == 'g') {
               IAR_L3_ON = "";
               IAR_L3_OFF = "style='background-color:red;'";
               IAR_ERROR = "";
             }
           else (IAR_ERROR = "LED3 error");
         }
/***************************************************************************************
                         IAR LED4:
****************************************************************************************/
         else if ( strcmp(cmd,"?IAR_L4=ON") == 0 )
         {
           Serial1.write('K');
           Serial.println("'K' sent...");
           Serial1.write('J');
           Serial.println("'J' sent...");
           while (Serial1.available()) {
             IAR_L4_STATUS = Serial1.read();
             Serial.print("IAR_L4_STATUS = ");
             Serial.println(IAR_L4_STATUS);
             }
           if (IAR_L4_STATUS == 'J') {
               IAR_L4_ON = "style='background-color:green;'";
               IAR_L4_OFF = "";
               IAR_ERROR = "";
             }
           else (IAR_ERROR = "LED4 error");
         }
         else if ( strcmp(cmd,"?IAR_L4=OFF") == 0 )
         {
           Serial1.write('L');
           Serial.println("'L' sent...");
           Serial1.write('J');
           Serial.println("'J' sent...");
           while (Serial1.available()) {
             IAR_L4_STATUS = Serial1.read();
             Serial.print("IAR_L4_STATUS = ");
             Serial.println(IAR_L4_STATUS);
             }
           if (IAR_L4_STATUS == 'j') {
               IAR_L4_ON = "";
               IAR_L4_OFF = "style='background-color:red;'";
               IAR_ERROR = "";
             }
           else (IAR_ERROR = "LED4 error");
         }
/***************************************************************************************
                         IAR DISCO:
****************************************************************************************/
         //Disco on, turn on and of all leds. While-loop runs 10 cycles. 
         else if ( strcmp(cmd,"?IAR_DISCO=ON") == 0 )
         {
           int iard = 1;
           while (iard < 10) {
             Serial1.write('B');
             delay(50);
             Serial1.write('C');
             delay(50);
             Serial1.write('E');
             delay(50);
             Serial1.write('F');
             delay(50);
             Serial1.write('H');
             delay(50);
             Serial1.write('I');
             delay(50);
             Serial1.write('K');
             delay(50);
             Serial1.write('L');
             delay(50);
             iard++;
           }
         }
                    //HTTP response header
/***************************************************************************************/ 
          CheckIarLed();
          // send a standard http response header
          Serial.println("");
          Serial.println("Sending HTTP response header...");
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
/***************************************************************************************
                         WEBPAGE:
****************************************************************************************/          
                         //HTML-header
          Serial.println("Sending Webpage...");
          Serial.println("");
          client.print("<!DOCTYPE HTML>\n<html>\n");
          client.print("<head>\n"
                         "<title>Arduino Remote Control</title>\n"
                         "<meta name='author' content='Grupp2' />\n" 
                         "<meta name='description' content='arduino webserver' />\n"
                         "<meta http-equiv='content-type' content='text/html; charset=UTF-8' />\n"
                         "<style type='text/css'>\n"
                         "#page {webkit-box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;\n font-family: sans; max-width: 600px; margin: 0 auto;}\n"
                         "#header {text-align:center;}\n"
                         "</style>\n"
                       "</head>\n");
/***************************************************************************************/
                       //HTML-body
          client.print("<body>\n");
          client.print("<div id='page'>\n");
          client.print("<div id='header'>\n"
                         "<h1 style='margin:auto;'>Arduino Remote control</h1>\n"
                         "<hr />\n"
                         "<br />\n"
                       "</div><!--/header-->\n");
          client.print("<div id='content'>\n"
                         "<form method='get' name='homepage'>\n"
                           "<fieldset>\n"
                             "<legend>Temp</legend>\n" 
                             "<p>"
                             "<b>Sensor 1:&nbsp;&nbsp;</b>");
                client.print(IAR_TEMP);
                client.print("&deg;C\n"
                             "</p>"
                           "</fieldset>\n"
                           "<fieldset>\n"
                             "<legend>Arduino</legend>\n"
/***************************Arduino LED1*************************************************/                             
                             "<div id='Lamp1'>\n"
                               "<p>\n"
                               "<b>LED 1&nbsp;&nbsp;&nbsp;</b>\n"
                               "<input type='submit' name='L1' id='L1_ON' value='ON' "); 
                  client.print(AR_L1_ON);
                  client.print("/>\n"
                               "<input type='submit' name='L1' id='L1_OFF' value='OFF' ");
                  client.print(AR_L1_OFF); 
                  client.print("/>\n"
                               "</p>\n"
                             "</div><!--/Lamp1-->\n"
/****************************Arduino LED2*************************************************/                             
                             "<div id='Lamp2'>\n"
                               "<p>\n"
                               "<b>LED 2&nbsp;&nbsp;&nbsp;</b>\n"
                               "<input type='submit' name='L2' id='L2_ON' value='ON' ");
                  client.print(AR_L2_ON);
                  client.print("/>\n"
                               "<input type='submit' name='L2' id='L2_OFF' value='OFF' ");
                  client.print(AR_L2_OFF);
                  client.print("/>\n"
                               "</p>\n"
                             "</div><!--/Lamp2-->\n"
/****************************Arduino LED3*************************************************/                             
                             "<div id='Lamp3'>\n"
                               "<p>\n"
                               "<b>LED 3&nbsp;&nbsp;&nbsp;</b>\n"
                               "<input type='submit' name='L3' id='L3_ON' value='ON' ");
                  client.print(AR_L3_ON);
                  client.print("/>\n"
                               "<input type='submit' name='L3' id='L3_OFF' value='OFF' ");
                  client.print(AR_L3_OFF);
                  client.print("/>\n"
                               "</p>\n"
                             "</div><!--/Lamp3-->\n"
/****************************Arduino LED4*************************************************/                             
                             "<div id='Lamp4'>\n"
                               "<p>\n"
                               "<b>LED 4&nbsp;&nbsp;&nbsp;</b>\n"
                               "<input type='submit' name='L4' id='L4_ON' value='ON' ");
                  client.print(AR_L4_ON);
                  client.print("/>\n"
                               "<input type='submit' name='L4' id='L4_OFF' value='OFF' ");
                  client.print(AR_L4_OFF);
                  client.print("/>\n"
                               "</p>\n"
                             "</div><!--/Lamp4-->\n"
                             "<div id='ar_error'>\n"
                               "<p>\n"
                               "Debugg: ");
                   client.print(AR_ERROR);
                   client.print("</p>\n"
                             "</div><!--/ar_error-->\n"
                           "</fieldset>\n"
                           "<fieldset>\n"
                             "<legend>IAR</legend>\n"
/****************************IAR LED1*************************************************/                             
                             "<div id='LED1'>\n"
                               "<p>\n"
                               "<b>LED 1&nbsp;&nbsp;&nbsp;</b>\n"
                               "<input type='submit' name='IAR_L1' id='IAR_L1_ON' value='ON' ");
                  client.print(IAR_L1_ON);             
                  client.print("/>\n"
                               "<input type='submit' name='IAR_L1' id='IAR_L1_OFF' value='OFF' ");
                  client.print(IAR_L1_OFF);
                  client.print("/>\n"
                               "</p>\n"
                             "</div><!--/LED1-->\n"
/****************************IAR LED2*************************************************/ 
                             "<div id='LED2'>\n"
                               "<p>\n"
                               "<b>LED 2&nbsp;&nbsp;&nbsp;</b>\n"
                               "<input type='submit' name='IAR_L2' id='IAR_L2_ON' value='ON' ");
                  client.print(IAR_L2_ON);
                  client.print("/>\n"
                               "<input type='submit' name='IAR_L2' id='IAR_L2_OFF' value='OFF' ");
                  client.print(IAR_L2_OFF);
                  client.print("/>\n"
                               "</p>\n"
                             "</div><!--/LED2-->\n"
/****************************IAR LED3*************************************************/                                                          
                             "<div id='LED3'>\n"
                               "<p>\n"
                               "<b>LED 3&nbsp;&nbsp;&nbsp;</b>\n"
                               "<input type='submit' name='IAR_L3' id='IAR_L3_ON' value='ON' ");
                  client.print(IAR_L3_ON);
                  client.print("/>\n"
                               "<input type='submit' name='IAR_L3' id='IAR_L3_OFF' value='OFF' ");
                  client.print(IAR_L3_OFF);
                  client.print("/>\n"
                               "</p>\n"
                             "</div><!--/LED3-->\n"
/****************************IAR LED4*************************************************/                                               
                             "<div id='LED4'>\n"
                               "<p>\n"
                               "<b>LED 4&nbsp;&nbsp;&nbsp;</b>\n"
                               "<input type='submit' name='IAR_L4' id='IAR_L4_ON' value='ON' ");
                  client.print(IAR_L4_ON);
                  client.print("/>\n"
                               "<input type='submit' name='IAR_L4' id='IAR_L4_OFF' value='OFF' ");
                  client.print(IAR_L4_OFF);
                  client.print("/>\n"
                               "</p>\n"
                             "</div><!--/LED4-->\n"
/****************************IAR DISCO*************************************************/                             
                             "<div id='IAR_DISCO'>\n"
                               "<p>\n"
                               "<b>Disco &nbsp;&nbsp;&nbsp;</b>\n"
                               "<input type='submit' name='IAR_DISCO' id='IAR_DISCO_ON' value='ON'/>\n"
                               "</p>\n"
                             "</div><!--/IAR_DISCO-->\n"
                             "<div id='iar_error'>\n"
                               "<p>"
                               "Debugg: \n");
                  client.print(IAR_ERROR);
                  client.print("</p>\n"
                             "</div><!--/iar_error-->\n"
                           "</fieldset>\n"
                         "</form>\n"
                       "</div><!--/content-->\n"
                     "</div><!--/page-->\n");
        client.print("</body>\n");
        client.print("</html>\n");                        
          //End of HTML-webpage
          
        break;
        }
        // If new line is recieved, set boolean true.
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        //If there is characters to read, ad them to buffer
        else if (bufferSize < bufferMax) {
        currentLineIsBlank = false;
        buffer[bufferSize++] = c; 
        } 
      }
    }
    
    // give the web browser time to receive the data
    delay(10);
    // close the connection:
    client.stop();
  }
}
/***************************************************************************************
                         ParseReceivedRequest()
****************************************************************************************/
void ParseReceivedRequest()
{
  //For debugging
  Serial.println("In ParseReceivedRequest");
  Serial.println(buffer);
  
  //Received buffer contains "GET /cmd/param1/param2 HTTP/1.1".  Break it up.
  char* slash1;
  //char* slash2;
  //char* slash3;
  char* space2;
  
  //For debugging
  Serial.println("variables inicilized..");
  Serial.println("Look for first slash");
  
  slash1 = strstr(buffer, "/") + 1; // Look for first slash
    //Serial.println("Look for second slash");
  //slash2 = strstr(slash1, "/") + 1; // second slash
    //Serial.println("Look for third slash");
  //slash3 = strstr(slash2, "/") + 1; // third slash
  Serial.println("Space after second slash...");
  space2 = strstr(slash1, " ") + 1; 
  //if (slash1 > space2) slash2=slash1;
  
  Serial.println("URL splitted.."); //For debugging
  
  //Print different parts of URL using function PrintString()
  PrintString("slash1",slash1);
  //PrintString("slash2",slash2);
  //PrintString("slash3",slash3);
  PrintString("space2",space2);
  
  
  // strncpy does not automatically add terminating zero, but strncat does! So start with blank string and concatenate.
  cmd[0] = 0;
  //param1[0] = 0;
  //param2[0] = 0;
  strncat(cmd, slash1, space2-slash1-1);
  //strncat(param1, slash2, slash3-slash2-1);
  //strncat(param2, slash3, space2-slash3-1);
  
  PrintString("cmd",cmd); //Print the actuall command using function PrintString()
  //PrintString("param1",param1);
  //PrintString("param2",param2);
  
}
/***************************************************************************************
                         PrintString()
****************************************************************************************/
//Just printing with a nice formated text
void PrintString(char* label, char* str)
{
  Serial.print(label);
  Serial.print("=");
  Serial.println(str);
}
/***************************************************************************************
                         CheckLedStatus()
****************************************************************************************/
void CheckLedStatus() 
{
  Serial.println("In CheckLedStatus()...");
  //Status for LED1
  if (digitalRead(8) == HIGH) {
    AR_L1_ON = "style='background-color:green;'";
    AR_L1_OFF = "";
  }
  else {
    AR_L1_ON = "";
    AR_L1_OFF = "style='background-color:red;'"; 
  }
  //Status for LED2
  if (digitalRead(9) == HIGH) {
    AR_L2_ON = "style='background-color:green;'";
    AR_L2_OFF = "";
  }
  else {
    AR_L2_ON = "";
    AR_L2_OFF = "style='background-color:red;'"; 
  }
  // Status for LED3
  if (digitalRead(10) == HIGH) {
    AR_L3_ON = "style='background-color:green;'";
    AR_L3_OFF = "";
  }
  else {
    AR_L3_ON = "";
    AR_L3_OFF = "style='background-color:red;'"; 
  }
  //Status for LED4
  if (digitalRead(11) == HIGH){
    AR_L4_ON = "style='background-color:green;'";
    AR_L4_OFF = "";
  }
  else {
    AR_L4_ON = "";
    AR_L4_OFF = "style='background-color:red;'"; 
  }
  
}

void CheckTemp() {
  Serial.println("In CheckTemp()...");
  Serial1.write('M');
  Serial.print("'M' sent... ");
  while (Serial1.available()) {
    IAR_TEMP[i] = Serial1.read();
    i++;
    if(i > 3) {
      i = 0;
      Serial.print("Temp is: ");
      Serial.println(IAR_TEMP);
      Serial.println("");
    }
  }
}
/*******************************************************************
                         CheckIarLed()
*******************************************************************/
void CheckIarLed() {
  
/***********************LED 1****************************/

  Serial1.write('A');
  Serial.println("'A' sent...");
  while (Serial1.available()) {
    IAR_L1_STATUS = Serial1.read();
    Serial.print("IAR_L1_STATUS = ");
    Serial.println(IAR_L1_STATUS);
  }
  if(IAR_L1_STATUS == 'A') {
    IAR_L1_ON = "style='background-color:green;'";
    IAR_L1_OFF = ""; 
    IAR_ERROR = "";
  }
  else if (IAR_L1_STATUS == 'a') {
    IAR_L1_ON = "";
    IAR_L1_OFF = "style='background-color:red;'"; 
    IAR_ERROR = "";
  }
  else (IAR_ERROR = "LED1 error");
  
/***********************LED 2****************************/

  Serial1.write('D');
  Serial.println("'D' sent...");
  while (Serial1.available()) {
    IAR_L2_STATUS = Serial1.read();
    Serial.print("IAR_L2_STATUS = ");
    Serial.println(IAR_L2_STATUS);
  }
  if (IAR_L2_STATUS == 'D') {
    IAR_L2_ON = "style='background-color:green;'";
    AR_L2_OFF = "";
    IAR_ERROR = "";
  }
  else if (IAR_L2_STATUS == 'd') {
    IAR_L2_ON = "";
    IAR_L2_OFF = "style='background-color:red;'";
    IAR_ERROR = "";  
  }
  else (IAR_ERROR = "LED2 error");
  
/***********************LED 3****************************/

  Serial1.write('G');
  Serial.println("'G' sent...");
  while (Serial1.available()) {
    IAR_L3_STATUS = Serial1.read();
    Serial.print("IAR_L3_STATUS = ");
    Serial.println(IAR_L3_STATUS);
  }
  if (IAR_L3_STATUS == 'G') {
    IAR_L3_ON = "style='background-color:green;'";
    IAR_L3_OFF = "";
    IAR_ERROR = "";
  }
  else if (IAR_L3_STATUS == 'g') {
    IAR_L3_ON = "";
    IAR_L3_OFF = "style='background-color:red;'";
    IAR_ERROR = "";
  }
  else (IAR_ERROR = "LED3 error"); 
  
/***********************LED 4****************************/

  Serial1.write('J');
  Serial.println("'J' sent...");
  while (Serial1.available()) {
    IAR_L4_STATUS = Serial1.read();
    Serial.print("IAR_L4_STATUS = ");
    Serial.println(IAR_L4_STATUS);
  }
  if (IAR_L4_STATUS == 'J') {
    IAR_L4_ON = "style='background-color:green;'";
    IAR_L4_OFF = "";
    IAR_ERROR = "";
  }
  else if (IAR_L4_STATUS == 'j') {
    IAR_L4_ON = "";
    IAR_L4_OFF = "style='background-color:red;'";
    IAR_ERROR = "";
  }
  else (IAR_ERROR = "LED4 error");
}
